package com.ljt.base.http

import com.ljt.base.bean.RandomCommentDataBean
import com.ljt.base.bean.RandomMusicDataBean
import com.ljt.base.bean.Repo
import com.ljt.base.http.HttpUrl.Companion.baseUrl
import com.ljt.base.http.HttpUrl.Companion.retrofitUrl
import okhttp3.OkHttpClient
import retrofit2.Call
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

/**
 * @auther :lujiantao
 * @date :2024/4/25 9:07 AM
 * @desc :请求联网、接收返回值
 */
class HttpAction {

    lateinit var url: String

    private val retrofit: Retrofit by lazy {

        val vicInterceptor = VicInterceptor()
        val okHttpClient = OkHttpClient().newBuilder().addInterceptor(vicInterceptor).build()

        Retrofit
            .Builder()
            //设置网络请求BaseUrl地址
            .baseUrl(url)
            //设置数据解析器
            .addConverterFactory(GsonConverterFactory.create())
            //添加日志拦截器
            .client(okHttpClient)
            .build()
    }

    /**
     * 热歌榜
     */
    fun getRandomMusicByCoroutine(sort: String, format: String): Call<RandomMusicDataBean> {
        url = baseUrl
        val api: HttpApi = retrofit.create(HttpApi::class.java)
        return api.getRandomMusicByCoroutine(sort, format)
    }

    /**
     * 文本描述获取
     */
    fun getRandomCommentByCoroutine(format: String): Call<RandomCommentDataBean> {
        url = baseUrl
        val api: HttpApi = retrofit.create(HttpApi::class.java)
        return api.getRandomCommentByCoroutine(format)
    }

    /**
     * 获取列表信息
     * @param  octocat
     */
    fun findUserList(key: String): Call<List<Repo>> {
        url = retrofitUrl
        val service: HttpApi = retrofit.create(HttpApi::class.java)
        return service.listRepos(key)
    }

}